﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    public class IntegratedLibrarySystem
    {
        private readonly BaseLibrarySystem _librarySystem;
        private readonly BaseUserSystem _userSystem;
        public const string MAIN_MENU = "欢迎使用集成图书馆系统\n1. 图书馆系统操作\n2. 用户系统操作\n0. 退出系统";
        public const string MENU_LIBRARY = "图书管理菜单\n1. 添加书籍\n2. 删除书籍\n3. 借阅书籍\n4. 归还书籍\n5. 查询书籍\n6. 显示库存\n7. 查询用户借阅记录\n0. 退出系统";
        public const string MENU_USER = "用户菜单\n1. 用户注册\n2. 修改密码\n3. 管理员菜单\n0. 退出用户菜单";
        public const string MENU_ADMIN = "管理员菜单\n1. 添加用户\n2. 删除用户\n3. 密码重置\n0. 退出管理员菜单";

        public IntegratedLibrarySystem(ILibraryFactory libraryFactory)
        {
            _librarySystem = libraryFactory.CreateLibrarySystem();
            _userSystem = libraryFactory.CreateUserSystem();
        }

        public void Execute()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(MAIN_MENU);
                Console.Write("请选择操作 (0-2): ");

                var choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        EnterLibrarySystem();
                        break;
                    case "2":
                        EnterUserSystem();
                        break;
                    case "0":
                        Console.WriteLine("退出系统...");
                        return;
                    default:
                        Console.WriteLine("无效的选项，请重新选择!");
                        break;
                }
                Console.WriteLine("按任意键返回主菜单...");
                Console.ReadKey();
            }
        }

        void EnterLibrarySystem()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(MENU_LIBRARY);
                Console.Write("请选择操作 (0-7): ");

                var choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        _librarySystem.AddBookMenu();
                        break;
                    case "2":
                        _librarySystem.DeleteBookMenu();
                        break;
                    case "3":
                        _librarySystem.BorrowBookMenu();
                        break;
                    case "4":
                        _librarySystem.ReturnBookMenu();
                        break;
                    case "5":
                        _librarySystem.QueryBookMenu();
                        break;
                    case "6":
                        _librarySystem.DisplayInventory();
                        break;
                    case "7":
                        _librarySystem.QueryBorrowedBookMenu();
                        break;
                    case "0":
                        Console.WriteLine("退出系统...");
                        return;
                    default:
                        Console.WriteLine("无效的选项，请重新选择!");
                        break;
                }

                Console.WriteLine("按任意键返回主菜单...");
                Console.ReadKey();
            }
        }

        void EnterUserSystem()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(MENU_USER);
                Console.Write("请选择操作 (0-3): ");

                var choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        _userSystem.AddUserMenu();
                        break;
                    case "2":
                        _userSystem.ChangePasswordMenu();
                        break;
                    case "3":
                        EnterAdminMenu();
                        break;
                    case "0":
                        Console.WriteLine("退出系统...");
                        return;
                    default:
                        Console.WriteLine("无效的选项, 请重新选择!");
                        break;
                }
                Console.WriteLine("按任意键返回主菜单...");
                Console.ReadKey();
            }
        }

        void ExecuteByAdmin()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(MENU_ADMIN);
                Console.Write("请选择操作 (0-3): ");

                var choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        _userSystem.AddUserMenu(RoleConstants.ADMIN);
                        break;
                    case "2":
                        _userSystem.DeleteUserMenu();
                        break;
                    case "3":
                        _userSystem.ResetPasswordMenu();
                        break;
                    case "0":
                        Console.WriteLine("退出管理员菜单...");
                        return;
                    default:
                        Console.WriteLine("无效的选项, 请重新选择!");
                        break;
                }

                Console.WriteLine("按任意键返回主菜单...");
                Console.ReadKey();
            }
        }

        void EnterAdminMenu()
        {
            var flag = _userSystem.JudgeAdminConsole().Item1;
            if (flag) ExecuteByAdmin();
        }
    }
}
